from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import random
db=SQLAlchemy()
class User(db.Model):
    __tablename__='user'
     
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False,index=True)
    password = db.Column(db.String(60), nullable=False,default=f"{random.randint(1000000000,90000000009000000000)}")
    role = db.Column(db.String(20), default='user') 
    classname=db.Column(db.String(20),)
  
class UserInfo(db.Model):#个人信息表
    __tablename__='userinfo'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20),db.ForeignKey('user.username',ondelete='SET NULL'),index=True)
    age=db.Column(db.Integer)
    gender=db.Column(db.Boolean)#true为男
    phone=db.Column(db.String(100))

class college(db.Model):#学校表
    __tablename__ = 'college'
    id=db.Column(db.Integer, primary_key=True,index=True)
    collegename=db.Column(db.String(120),nullable=False,index=True)#学院名称
    major = db.Column(db.String(100),index=True,unique=True)#专业名称
    timestamp = db.Column(db.String(100), default=datetime.now().strftime('%Y-%m-%d %H:%M'))
 
class classInfo(db.Model):#班级信息
    __tablename__='classInfo'
    id=db.Column(db.Integer, primary_key=True)
    major = db.Column(db.String(100), db.ForeignKey('college.major',ondelete='SET NULL'))#专业名称
    classname=db.Column(db.String(100),nullable=False)#班级名称
    timestamp = db.Column(db.String(100), default=datetime.now().strftime('%Y-%m-%d %H:%M'))
    userid=db.Column(db.Integer, db.ForeignKey('user.id',ondelete='SET NULL'))
class course(db.Model):#课程表
    __tablename__ = 'course'
    id=db.Column(db.Integer,primary_key=True)
    coursename=db.Column(db.String(50),index=True)#课程名称
    timestamp = db.Column(db.String(100), default=datetime.now().strftime('%Y-%m-%d %H:%M'))
    classid=db.Column(db.Integer,db.ForeignKey('classInfo.id',ondelete='SET NULL'))#班级名称
    teacher=db.Column(db.String(100),db.ForeignKey('user.username',ondelete='SET NULL'))
    messages=db.Column(db.String(200),nullable=False)
class statuscourse(db.Model):#已选择的课程
    __tablename__ = 'statuscourse'
    id=db.Column(db.Integer,primary_key=True)
    username=db.Column(db.String(100),db.ForeignKey('user.username',ondelete='SET NULL'))
    courseid=db.Column(db.Integer,db.ForeignKey('course.id',ondelete='SET NULL'))#课程名称
class messageInfo(db.Model):
    __tablename__ = 'messageInfo'
    id=db.Column(db.Integer,primary_key=True)
    username=db.Column(db.String(100),db.ForeignKey('user.username',ondelete='SET NULL'))
    message=db.Column(db.String(100),nullable=False)
    chatmessage=db.Column(db.String(1000),nullable=False)
    value=db.Column(db.Integer,nullable=False)
class uniquevalue(db.Model):
    __tablename__ = 'uniquevalue'
    id=db.Column(db.Integer,primary_key=True)
    courseid=db.Column(db.Integer,db.ForeignKey('course.id',ondelete='SET NULL'))#课程名称
    userid=db.Column(db.Integer,db.ForeignKey('user.id',ondelete='SET NULL'))
    number=db.Column(db.Integer,nullable=False)
    timestamp =db.Column(db.String(100), default=datetime.now().strftime('%Y-%m-%d'))
